package com.manager.core.module.manager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.manager.commons.mybatis.service.GenericService;
import com.manager.core.module.manager.dao.PlatformRolePermissionDao;
import com.manager.core.module.manager.entity.ComboTreeList;
import com.manager.core.module.manager.entity.article.GeneralTopicCategory;
import com.manager.core.web.json.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manager.commons.utils.StringUtil;
import com.manager.commons.base.DataGrid;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.core.module.manager.dao.PlatformPermissionDao;
import com.manager.core.module.manager.entity.PlatformPermission;
import org.springframework.transaction.annotation.Transactional;

/**
 * PlatformPermissionService
 *
 * @author H
 * @version 1.0
 */
@Service
public class PlatformPermissionService extends GenericService<PlatformPermission> {

    @Autowired
    private PlatformPermissionDao jdbcDao;
    @Autowired
    private PlatformRolePermissionDao rolePermissionDao;

    @Override
    public GenericDao<PlatformPermission> getDao() {
        return jdbcDao;
    }

    @Override
    public PlatformPermission getModel() {
        return new PlatformPermission();
    }

    /**
     * 销毁
     *
     * @param params
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public AjaxJson destroyAndRole(Map<String, Object> params) throws Exception {
        try {
            if (null != params) {
                rolePermissionDao.destroyPpCode(getParamsString(params, "_id"));
                int f = destroyMap(params);
                return new AjaxJson(f, "操作".concat(f > 0 ? "成功" : "失败"));
            }
            return new AjaxJson("异常错误，请稍后再试");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e);
        }

    }


    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    public DataGrid queryPage(Map<String, Object> params) {
        try {
            if (null != params) {
                int count = jdbcDao.countParent(params);
                List<PlatformPermission> list = jdbcDao.queryParent(params);
                queryChildren(list, params);
                return new DataGrid(count, list);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new DataGrid(0, new ArrayList<GeneralTopicCategory>());
    }

    private void queryChildren(List<PlatformPermission> list, Map<String, Object> params) {
        for (PlatformPermission gtc : list) {
            params.put("pp_parent", gtc.getPpCode());
            List<PlatformPermission> l = jdbcDao.queryParent(params);
            if (l != null && !l.isEmpty()) {
                gtc.setChildren(l);
                queryChildren(l, params);
            }
        }
    }

    @Override
    public AjaxJson save(Map<String, Object> params) {
        String code = getParamsString(params, "ppCode");
        if (StringUtil.isNotEmpty(code)) {
            params.put("ppAuth", code.replace("-", ":"));
        }
        int ppSort = getParamsInt(params, "ppSort");
        params.put("ppSort", ppSort);
        return super.save(params);
    }
    public List<ComboTreeList> queryTreeList() {
        return jdbcDao.queryTree();
    }

}